package com.sy.simpleegg.ui.wifistateview

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import com.sy.simpleegg.R

class WiFiStateView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private var wifi_state = 3
    private var height = 0
    private var width = 0
    private var paint: Paint? = null
    private var startAngle = 0
    private var sweepAngle = 0
    private var wifiHeight = 0
    private var padding_bottom = 0
    private var bottom_x = 0
    private var bottom_y = 0

    private var signalMaximum: Int = 5

    /**
     * signal pole unit width, default value: 5
     */
    private var unitWidth: Int = 5

    @ColorInt
    private var primaryColor: Int = Color.BLACK

    /**
     * signal pole level color, default color: white
     */
    @ColorInt
    private var levelColor: Int = Color.WHITE

    /**
     * signal pole shadow color, default color: gray
     */
    @ColorInt
    private var shadowColor: Int = Color.GRAY

    enum class Style {
        RECT,
        ROUND
    }

    private var style = Style.ROUND

    init {
        setAttributeSet(context, attrs)
        init()
    }

    private fun setAttributeSet(context: Context, attrs: AttributeSet?) {
        context.obtainStyledAttributes(attrs, R.styleable.WifiView).also {
            initValue(it)
        }.recycle()
    }

    private fun initValue(typedArray: TypedArray) {
        typedArray.apply {
            signalMaximum = getInt(R.styleable.WifiView_wifi_signal_maximum, signalMaximum)
//            signalLevel = getInt(R.styleable.SignalView_signal_level, signalLevel)
            primaryColor = getColor(R.styleable.WifiView_wifi_primary_color, primaryColor)
            levelColor = getColor(R.styleable.WifiView_wifi_level_color, levelColor)
//            spacing = getInt(R.styleable.SignalView_spacing, spacing)
//            unitWidth = getInt(R.styleable.SignalView_unit_width, unitWidth)
//            connected = getBoolean(R.styleable.SignalView_connected, connected)
//            shadowColor = getColor(R.styleable.SignalView_shadow_color, shadowColor)
//            shadowOpen = getBoolean(R.styleable.SignalView_shadow_open, shadowOpen)
        }
    }

    private fun init() {
        paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint!!.color = Color.BLACK
        paint!!.isAntiAlias = true
        shadowSwitch(paint!!)
    }

    private fun shadowSwitch(paint: Paint) {
        if (true) {
//            val shadowWith = (unitWidth * 0.2).toFloat()
            val unitWidth = wifiHeight / signalMaximum
            val shadowWith = (unitWidth * 0.8).toFloat()
            paint.setShadowLayer(2F, 0F, 3F, shadowColor)
            setLayerType(LAYER_TYPE_SOFTWARE, null)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //获取宽的测量模式
        val heightSize = MeasureSpec.getSize(heightMeasureSpec) //测量值
        height = resolveSize(heightSize, heightMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec) //测量值
        width = resolveSize(widthSize, widthMeasureSpec)
        val calc_wifi_height = (width / Math.sqrt(2.0)).toInt()
        wifiHeight = Math.min(calc_wifi_height, height)
        padding_bottom = (height - wifiHeight) / 2
        bottom_x = width / 2
        bottom_y = height - padding_bottom
        setMeasuredDimension(width, height)
    }

    fun setWifi_state(level: Int) {
//        wifi_state = WifiManager.calculateSignalLevel(level, signalMaximum)
        wifi_state = level
        postInvalidate()
    }

    fun setStyle(style: Style) {
        this.style = style
        postInvalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val signalRadius = wifiHeight / signalMaximum
        val paint_width = signalRadius / 2
        paint!!.strokeWidth = paint_width.toFloat()
        if (style == Style.RECT) {
            paint!!.strokeCap = Paint.Cap.BUTT
            startAngle = -135
            sweepAngle = 90
        } else {
            paint!!.strokeCap = Paint.Cap.ROUND
            startAngle = -130
            sweepAngle = 80
        }

        for (i in 1..signalMaximum) {
            val radius = (signalRadius * i).toFloat()
            if (i <= wifi_state) {
//                paint.setColor(Color.parseColor("#4b4b4b"));
                paint!!.color = levelColor
            } else {
//                paint.setColor(Color.parseColor("#c9c9c9"));
                paint!!.color = primaryColor
            }
            var rectf: RectF
            if (i == 1) {
                paint!!.style = Paint.Style.FILL
                if (style == Style.RECT) {
                    rectf = RectF(
                        bottom_x - radius,
                        bottom_y - radius,
                        bottom_x + radius,
                        bottom_y + radius
                    )
                    canvas.drawArc(rectf, startAngle.toFloat(), sweepAngle.toFloat(), true, paint!!)
                } else {
                    canvas.drawCircle(
                        bottom_x.toFloat(),
                        (bottom_y - paint_width).toFloat(),
                        paint_width.toFloat(),
                        paint!!
                    )
                }
            } else {
                paint!!.style = Paint.Style.STROKE
                rectf = RectF(
                    bottom_x - radius + paint_width / 2,
                    bottom_y - radius + paint_width / 2,
                    bottom_x + radius - paint_width / 2,
                    bottom_y + radius - paint_width / 2
                )
                canvas.drawArc(rectf, startAngle.toFloat(), sweepAngle.toFloat(), false, paint!!)
            }
        }
    }
}